import React from 'react';
import {render} from 'react-dom';
import {Provider} from 'react-redux';
import {Router, Route, IndexRedirect, useRouterHistory, Link,Lifecycle} from 'react-router';
import {createHistory, useBeforeUnload} from 'history'

import configureStore from './store/configureStore';

import PageNotFound from './views/PageNotFound'
import App from './views/App';
import Home from './views/Home';
import Login from './views/Login';

import Msg from './views/Msg';
import Reply from './views/Reply';

import ReplyFollow from './views/Reply/follow'
import ReplyAuto from './views/Reply/auto'
import ReplyKey from './views/Reply/key'

import Follower from './views/Follower';
import Menu from './views/Menu';
import AddWXSubMenu from './views/Menu/addWXSubMenu';
import AddSubMenu from './views/Menu/addWXMenu'

import Product from './views/Product';
import AddSeries from './views/Product/addSeries'
import EditSeries from './views/Product/editSeries'
import ProductCate from './views/Product/productCate'
import Products from './views/Product/products'
import AddProduct from './views/Product/addProduct'
import EditProduct from './views/Product/editProduct'

import Material from './views/Material';
import MaterialPoster from './views/Material/poster'
import MaterialPosterEdit from './views/Material/editPoster'
import MaterialAddPoster from './views/Material/addPoster'
import MaterialPic from  './views/Material/pic'
import MaterialPicEdit from './views/Material/editPic'
import MaterialAddPic from './views/Material/addPic'
import MaterialPage from './views/Material/page'
import MaterialPageEdit from './views/Material/editPage'
import MaterialPageAdd from './views/Material/addPage'

import Solution from './views/Solution'
import AddSolution from './views/Solution/addSolution'
import EditSolution from './views/Solution/editSolution'
import SolutionCate from './views/Solution/solutionCate'


import WhitePage from './views/WhitePage'
import Activity from './views/Activity'
import AddActivity from './views/Activity/addActivity'
import EditActivity from './views/Activity/editActivity'


import Support from './views/Support'
import Video from './views/Video'
import EditVideo from './views/Video/editVideo'

import Manage from './views/Manage'
//----------------------------------------------
import Chart from './views/Chart';

import Movie from './views/Movie'
import TestBind from './views/Movie/bind'
import TestRedirect from './views/Movie/redirect'
import TestBox from './views/Movie/box'
import TestTable from './views/Movie/table'

import Room from './views/Movie/room'


import {getCookie} from './utils';

const history = useRouterHistory(useBeforeUnload(createHistory))({basename: ''})
// history.listenBeforeUnload(function () {
//     return 'Are you sure you want to leave this page?'
// })

const store = configureStore();
console.log("ffffffffffff")
const validate = function (next, replace, callback) {
        const isLoggedIn = !!getCookie('uid')
     if (!isLoggedIn && next.location.pathname != '/login') {
          replace('/login')
     }
    console.log('---------------onEnter----------------------')
    callback()
}



render(
    <Provider store={store}>
        <Router history={history} >

            <Route path="/" onEnter={validate}>
                <IndexRedirect to="login"/>
                <Route component={App}>
                    <Route path="home" component={Home}/>
                    <Route path="msg" component={Msg}/>
                    <Route path="reply" component={Reply}>
                        <IndexRedirect to="follow"/>
                        <Route path="follow" component={ReplyFollow}></Route>
                        <Route path="auto" component={ReplyAuto}></Route>
                        <Route path="key" component={ReplyKey}></Route>
                    </Route>
                    <Route path="follower" component={Follower}/>
                    <Route path="menu" component={Menu}></Route>
                    <Route path="addWXSubMenu/:key" component={AddWXSubMenu} />
                    <Route path="addSubMenu" component={AddSubMenu}/>




                    <Route path="product" component={Product} />
                    <Route path="addSeries" component={AddSeries} />
                    <Route path="editSeries/:id" component={EditSeries} />
                    <Route path="productCate" component={ProductCate} />
                    <Route path="products/:categoryId/:seriesId" component={Products} />
                    <Route path="addProduct/:categoryId/:seriesId" component={AddProduct} />
                    <Route path="editProduct/:categoryId/:id" component={EditProduct} />


                    <Route path="material" component={Material} >
                        <Route path="poster" component={MaterialPoster}/>
                        <Route path="editPoster/:id" component={MaterialPosterEdit} />
                        <Route path="addPoster" component={MaterialAddPoster} />
                        <Route path="pic" component={MaterialPic} />
                        <Route path="editPic/:id" component={MaterialPicEdit} />
                        <Route path="addPic" component={MaterialAddPic} />
                        <Route path="page" component={MaterialPage} />
                        <Route path="editPage/:id" component={MaterialPageEdit} />
                        <Route path="addPage" component={MaterialPageAdd} />

                     </Route>

                    <Route path="solution" component={Solution} />
                    <Route path="addSolution" component={AddSolution} />
                    <Route path="editSolution/:id"  component={EditSolution} />
                    <Route path="solutionCate" component={SolutionCate}/>

                    <Route path="whitePage" component={WhitePage} />
                    <Route path="activity" component={Activity} />
                    <Route path="addActivity" component={AddActivity} />
                    <Route path="editActivity/:id" component={EditActivity} />
                    <Route path="support" component={Support} />

                    <Route path="manage" component={Manage}>

                    </Route>
                    <Route path="video" component={Video} />
                    <Route path="editVideo/:id" component={EditVideo} />



                    <Route path="movie" component={Movie}>
                        <Route path="bind" component={TestBind}/>
                        <Route path="redirect" component={TestRedirect}/>
                        <Route path="box" component={TestBox}/>
                        <Route path="table" component={TestTable}/>

                        <Route path="room" component={Room}/>
                    </Route>
                    <Route path="chart" component={Chart}/>


                </Route>

                <Route path="login" component={Login}/>

            </Route>
            <Route path="*" component={PageNotFound}/>
        </Router>
    </Provider>
    ,
    document.getElementById('root')
);
